/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.com.apresentacao.matricula;

import src.com.negocio.pedagogico.Turma;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import src.com.apresentacao.pedagogico.ConsultaTurma;
import src.com.negocio.matricula.AgendaAula;
import src.com.negocio.pedagogico.ConsultarTurma;

/**
 *
 * @author Giane
 */
public class AgendaAulaDemonstrativa extends javax.swing.JFrame {

    /**
     * Creates new form AgendaAulaDemonstrativa
     */
    public AgendaAulaDemonstrativa() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        n_nome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        c_cpf = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        t_turma = new javax.swing.JTextField();
        jToggleButton1 = new javax.swing.JToggleButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        d = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        pesquisa = new javax.swing.JToggleButton();
        d_dia = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jInternalFrame1.setTitle("Agedar Aulas Demonstrativas");
        jInternalFrame1.setVisible(true);

        jLabel1.setText("Alunio: ");

        jLabel2.setText("CPF:");

        c_cpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                c_cpfActionPerformed(evt);
            }
        });

        jLabel3.setText("Turma: ");

        jToggleButton1.setText("Agendar");
        jToggleButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jToggleButton1MouseClicked(evt);
            }
        });

        jRadioButton1.setSelected(true);
        jRadioButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButton1MouseClicked(evt);
            }
        });

        d.setText("Dia:");
        d.setEnabled(false);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null}
            },
            new String [] {
                "Turma", "Professor"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        pesquisa.setText("Pesquisar");
        pesquisa.setEnabled(false);
        pesquisa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pesquisaMouseClicked(evt);
            }
        });

        d_dia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado" }));
        d_dia.setEnabled(false);

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addGap(271, 271, 271)
                        .addComponent(jToggleButton1))
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jInternalFrame1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(n_nome, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jInternalFrame1Layout.createSequentialGroup()
                                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(d))
                                .addGap(18, 18, 18)
                                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                                        .addComponent(d_dia, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(pesquisa))
                                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                                        .addComponent(c_cpf, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(t_turma)))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jRadioButton1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(n_nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(c_cpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(t_turma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jRadioButton1))
                .addGap(18, 18, 18)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(d)
                    .addComponent(pesquisa)
                    .addComponent(d_dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jToggleButton1)
                .addContainerGap(59, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void c_cpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_c_cpfActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_c_cpfActionPerformed

    private void jToggleButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jToggleButton1MouseClicked
        String aluno = n_nome.getText().toString();
        String CPF = c_cpf.getText().toString();
        String turma = t_turma.getText().toString();

        Turma tur = new Turma();
        try {
            String ex = tur.verificaturma(turma);
            if (jRadioButton1.isSelected()) {
                if (!ex.equals("0")) {
                    ConsultarTurma t = new ConsultarTurma();
                    String id_turma = t.consTurma(turma);
                    AgendaAula ag = new AgendaAula();
                    ag.agendarAula(aluno, CPF, id_turma);
                    javax.swing.JDialog inf = new javax.swing.JDialog();
                    inf.setSize(250, 150);
                    javax.swing.JOptionPane.showMessageDialog(inf, "Agendamento realizado!", "Informação", javax.swing.JOptionPane.INFORMATION_MESSAGE);
                } else {
                    javax.swing.JDialog inf = new javax.swing.JDialog();
                    inf.setSize(250, 150);
                    javax.swing.JOptionPane.showMessageDialog(inf, "Turma informada não existe!", "Informação", javax.swing.JOptionPane.WARNING_MESSAGE);
                }
            } else {
                String cod = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
                String ex2 = tur.verificaturma(cod);
                if (!ex2.equals("0")) {
                    ConsultarTurma t = new ConsultarTurma();
                    String id_turma = t.consTurma(cod);
                    AgendaAula ag = new AgendaAula();
                    ag.agendarAula(aluno, CPF, id_turma);
                    javax.swing.JDialog inf = new javax.swing.JDialog();
                    inf.setSize(250, 150);
                    javax.swing.JOptionPane.showMessageDialog(inf, "Agendamento realizado!", "Informação", javax.swing.JOptionPane.INFORMATION_MESSAGE);

                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(AgendaAulaDemonstrativa.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_jToggleButton1MouseClicked

    private void jRadioButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButton1MouseClicked
        if (!jRadioButton1.isSelected()) {
            n_nome.setEnabled(true);
            c_cpf.setEnabled(true);
            t_turma.setEnabled(false);
            d.setEnabled(true);
            d_dia.setEnabled(true);
            pesquisa.setEnabled(true);
            jTable1.setEnabled(true);
            jScrollPane1.setEnabled(true);
        } else {
            n_nome.setEnabled(true);
            c_cpf.setEnabled(true);
            t_turma.setEnabled(true);
            d.setEnabled(false);
            d_dia.setEnabled(false);
            pesquisa.setEnabled(false);
            jTable1.setEnabled(false);
            jScrollPane1.setEnabled(false);
        }
    }//GEN-LAST:event_jRadioButton1MouseClicked

    private void pesquisaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pesquisaMouseClicked
        String aluno = n_nome.getText().toString();
        String CPF = c_cpf.getText().toString();
        String dia = d_dia.getSelectedItem().toString();

        Turma ag = new Turma();
        try {
            ArrayList<String> listaTurmas = ag.retornaTurmas(dia);

            if (!listaTurmas.isEmpty()) {

                jTable1.setModel(new javax.swing.table.DefaultTableModel(new String[]{
                    "Turma", "Professor"
                }, listaTurmas.size() / 2));//, 1));

                int i = 0;
                for (int j = 0; j < listaTurmas.size() / 2; j++) {
                    for (int z = 0; z < 2; z++) {
                        jTable1.setValueAt(listaTurmas.get(i), j, z);
                        i++;
                    }
                }

                jScrollPane1.setViewportView(jTable1);

            }

        } catch (SQLException ex) {
            Logger.getLogger(AgendaAulaDemonstrativa.class.getName()).log(Level.SEVERE, null, ex);
        }



    }//GEN-LAST:event_pesquisaMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AgendaAulaDemonstrativa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AgendaAulaDemonstrativa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AgendaAulaDemonstrativa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AgendaAulaDemonstrativa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AgendaAulaDemonstrativa().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField c_cpf;
    private javax.swing.JLabel d;
    private javax.swing.JComboBox d_dia;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JTextField n_nome;
    private javax.swing.JToggleButton pesquisa;
    private javax.swing.JTextField t_turma;
    // End of variables declaration//GEN-END:variables
}
